package com.ropeok.controller;

import com.ropeok.Result.Result;
import com.ropeok.entity.User;
import com.ropeok.entity.UserAndRole;
import com.ropeok.mapper.UserAndRoleMapper;
import com.ropeok.service.serviceimpl.ServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;


@Api(tags = "操作用户角色关系表")
@RestController
public class UserAndRoleController {

    @Autowired
    UserAndRoleMapper userAndRoleMapper;
    @Autowired
    ServiceImpl service;

    @ApiOperation(value = "增加信息",notes = "为某个用户赋予角色")
    @PostMapping(value = "/uar")
    public Result save(@Valid UserAndRole userAndRole, BindingResult bindingResult ){
        if (bindingResult.hasErrors()) {
            return Result.error(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        service.SaveUserAndRole(userAndRole);
        return Result.success();
    }

    @ApiOperation(value = "删除中间表信息",notes = "通过用户id删除中间表")
    @GetMapping(value = "/delUr{id}")
    public Result deletU(@PathVariable("id") Integer id){
        Result result;
        if(null==id){
            result=Result.error("id不能为空");
        }else {
            service.DelUserAndRoleByUser(id);
            result=Result.success();
        }
        return result;
    }

    @ApiOperation(value = "删除中间表信息",notes = "通过角色id删除中间表")
    @GetMapping(value = "/deluR{id}")
    public Result deletR(@PathVariable("id") Integer id){
        Result result;
        if(null==id){
            result=Result.error("id不能为空");
        }else {
            service.DelUserAndRoleByRole(id);
            result=Result.success();
        }
        return result;
    }

}
